<?php
/**
 * Class that operate on table 'businesses'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class BusinessesMySqlDAO implements BusinessesDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return BusinessesMySql 
	 */
	public function load($businesses){
		$sql = 'SELECT * FROM businesses WHERE business_id = "'.$businesses->business_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	
	
	public function businesscategory_list($businesses){
		$sql = 'SELECT businesscat_id, category_name FROM businesses_category';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllSearchBy($businesses){
		$sql = 'SELECT * FROM businesses WHERE '.$businesses->search_by.' like "%'.$businesses->search_by_value.'%" order by '.$businesses->order_by.' '.$businesses->sort_order.' limit '.$businesses->results_per_page.' offset '.$businesses->offset.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function SearchByCount($businesses){
		$sql = 'SELECT count(*) FROM businesses WHERE '.$businesses->search_by.' like "%'.$businesses->search_by_value.'%" order by '.$businesses->order_by.' '.$businesses->sort_order.' limit '.$businesses->results_per_page.' offset '.$businesses->offset.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	public function queryAll($businesses){
		$sql = 'SELECT * FROM businesses order by '.$businesses->order_by.' '.$businesses->sort_order.' limit '.$businesses->results_per_page.' offset '.$businesses->offset.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function queryAllCount($businesses){
		$sql = 'SELECT count(*) FROM businesses order by '.$businesses->order_by.' '.$businesses->sort_order.' limit '.$businesses->results_per_page.' offset '.$businesses->offset.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param businesses primary key
 	 */
	public function delete($businesses){
		$sql = 'UPDATE businesses SET status = "'.$businesses->status.'" WHERE business_id = "'.$businesses->business_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param BusinessesMySql businesses
 	 */
	public function insert($businesses){
		$sql = 'INSERT INTO businesses (business_name, businesscat_id, member, business_type, start_date, description, url, address, business_image, image_name, expire_in, created_date, updated_date) VALUES ("'.$businesses->business_name.'", "'.$businesses->businesscat_id.'", "'.$businesses->member.'", "'.$businesses->business_type.'", "'.$businesses->start_date.'", "'.$businesses->description.'", "'.$businesses->url.'", "'.$businesses->address.'", "'.$businesses->business_image.'", "'.$businesses->image_name.'", "'.$businesses->expire_in.'", "'.$businesses->created_date.'", "'.$businesses->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$businesses->business_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param BusinessesMySql businesses
 	 */
	public function update($businesses){
		$sql = 'UPDATE businesses SET business_name = "'.$businesses->business_name.'", businesscat_id = "'.$businesses->businesscat_id.'", member = "'.$businesses->member.'", business_type = "'.$businesses->business_type.'", start_date = "'.$businesses->start_date.'", description = "'.$businesses->description.'", url = "'.$businesses->url.'", address = "'.$businesses->address.'", business_image = "'.$businesses->business_image.'", image_name = "'.$businesses->image_name.'", expire_in = "'.$businesses->expire_in.'", status = "'.$businesses->status.'", updated_date = "'.$businesses->updated_date.'" WHERE business_id = "'.$businesses->business_id.'"';
		$sqlQuery = new SqlQuery($sql);
		//print_r($sql);exit;
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM businesses';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByBusinessName($value){
		$sql = 'SELECT * FROM businesses WHERE business_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCategoryId($value){
		$sql = 'SELECT * FROM businesses WHERE category_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMember($value){
		$sql = 'SELECT * FROM businesses WHERE member = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByBusinessType($value){
		$sql = 'SELECT * FROM businesses WHERE business_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStartDate($value){
		$sql = 'SELECT * FROM businesses WHERE start_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescription($value){
		$sql = 'SELECT * FROM businesses WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUrl($value){
		$sql = 'SELECT * FROM businesses WHERE url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAddress($value){
		$sql = 'SELECT * FROM businesses WHERE address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByBussinessImage($value){
		$sql = 'SELECT * FROM businesses WHERE bussiness_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM businesses WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM businesses WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM businesses WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByBusinessName($value){
		$sql = 'DELETE FROM businesses WHERE business_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCategoryId($value){
		$sql = 'DELETE FROM businesses WHERE category_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMember($value){
		$sql = 'DELETE FROM businesses WHERE member = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByBusinessType($value){
		$sql = 'DELETE FROM businesses WHERE business_type = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStartDate($value){
		$sql = 'DELETE FROM businesses WHERE start_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescription($value){
		$sql = 'DELETE FROM businesses WHERE description = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUrl($value){
		$sql = 'DELETE FROM businesses WHERE url = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAddress($value){
		$sql = 'DELETE FROM businesses WHERE address = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByBussinessImage($value){
		$sql = 'DELETE FROM businesses WHERE bussiness_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM businesses WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM businesses WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM businesses WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return BusinessesMySql 
	 */
	protected function readRow($row){
		$businesses = new Businesses();
		
		$businesses->business_id = $row['business_id'];
		$businesses->business_name = $row['business_name'];
		$businesses->businesscat_id = $row['businesscat_id'];
		$businesses->category_name = $row['category_name'];
		$businesses->member = $row['member'];
		$businesses->business_type = $row['business_type'];
		$businesses->start_date = $row['start_date'];
		$businesses->description = $row['description'];
		$businesses->url = $row['url'];
		$businesses->address = $row['address'];
		$businesses->business_image = $row['business_image'];
		$businesses->image_name = $row['image_name'];
		$businesses->expire_in = $row['expire_in'];
		$businesses->status = $row['status'];
		$businesses->created_date = $row['created_date'];
		$businesses->updated_date = $row['updated_date'];

		return $businesses;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return BusinessesMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>